<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="600px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" inline label-width="100px" label-suffix=":">
            <el-form-item label="平台" prop="platform_id">
                    <el-select v-model="model.platform_id" placeholder="请选择平台" clearable>
                        <el-option v-for="(item, index) in dict.platformList" :key="index" :label="item.title" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            <el-form-item label="花费" prop="spend">
                <el-input v-model.trim="model.spend" placeholder="请输入花费" clearable></el-input>
            </el-form-item>
            <el-form-item label="日期" prop="createtime">
                <el-date-picker v-model="model.createtime" type="date" placeholder="请选择日期" clearable> </el-date-picker>
            </el-form-item>
            <template v-if="type == 'add'">
                <el-form-item label="广告账户" prop="advertiser_id">
                    <!-- <el-input v-model.trim="model.advertiser_id" placeholder="请输入广告账户" clearable></el-input> -->
                    <el-select v-model="model.advertiser_id" placeholder="请选择广告账户" clearable filterable>
                        <el-option v-for="(item, index) in dict.advistementList" :key="index" :label="item.advertiser_id" :value="item.advertiser_id"></el-option>
                    </el-select>
                </el-form-item>
                    
            </template>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { queryAdvplatformList, getAdvistementList, advcampaignFind, advcampaignAdd, advcampaignEdit } from '@/api/advertisement'
export default {
    name: 'DetailDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
        type: {
            type: String,
            default: '',
        },
    },
    data() {
        return {
            model: {
                id: '',
                advertiser_id: '',
                platform_id: '',
                createtime: '',
                spend: undefined,
            },
            rules: {
                advertiser_id: [{ required: true, message: '广告账户不能为空', trigger: ['blur', 'change'] }],
                spend: [{ required: true, message: '花费不能为空', trigger: ['blur', 'change'] }],
                platform_id: [{ required: true, message: '平台不能为空', trigger: ['blur', 'change'] }],
                createtime: [{ required: true, message: '日期不能为空', trigger: ['blur', 'change'] }],
            },
            dict: {
                platformList: [],
                advistementList: []
            },
            save: false,
        }
    },
    computed: {
        title() {
            return `${this.type == 'edit' ? '编辑' : '新增'}广告账户花费`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    this.getDetail()
                }
            },
            immediate: true,
            deep: true,
        },
        'model.platform_id': {
            handler(val) {
                if (val) {
                    this.getAdvistementList()
                }
            },
            immediate: true,
            deep: true,
        }
    },
    async mounted() {
        try {
            const [platformData] = await Promise.all([queryAdvplatformList()])
            this.dict.platformList = platformData?.data?.list ?? []
        } catch (error) {
            console.log(error)
        }
    },
    methods: {
        getAdvistementList() {
            getAdvistementList({platform_id: this.model.platform_id}).then(res=>{
                this.dict.advistementList = res.data ?? []
            })
        },
        getDetail() {
            this.loading = true
            const params = { id: this.params.id }
            advcampaignFind(params)
                .then(res => {
                    this.model = Object.assign({}, this.model, res.data)
                })
                .finally(() => {
                    this.loading = false
                })
        },
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }
                const params = { ...this.model }
                if (this.save) {
                    return
                }
                this.save = true
                if (this.type === 'edit') {
                    advcampaignEdit(params)
                        .then(res => {
                            this.$message.success(res.msg)
                            this.dialogVisible = false
                            this.$emit('success')
                        })
                        .finally(() => {
                            this.save = false
                        })
                } else {
                    advcampaignAdd(params)
                        .then(res => {
                            this.$message.success(res.msg)
                            this.dialogVisible = false
                            this.$emit('success')
                        })
                        .finally(() => {
                            this.save = false
                        })
                }
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style>
</style>
